package com.leador.location;

import android.content.Context;
import android.util.Log;

import com.leador.assist.DataQueueManager;
import com.leador.jni.TraceJniInterface;
import com.leador.manger.LBSTraceClient;
import com.leador.module.TrackCustom;
import com.leador.service.LBSTraceService;
import com.leador.utils.JsonTool;
import com.leador.utils.JsonUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by lhy on 2016/9/12.
 */
public class LocateHelper {
    public    int gatherInterval = 5000;  //默认5秒
    public    int baseGatherInterval = 2;
    public    int distanceInterval = 0;
    private   LBSTraceService lbsTraceService;
    private  static LocationListener locationListener;

    public int getGatherInterval() {
        return gatherInterval;
    }

    public void setGatherInterval(int gatherInterval) {
        this.gatherInterval = gatherInterval;
    }

    public void setDistanceInterval(int distanceInterval) {
        this.distanceInterval = distanceInterval;
    }

    public LocateHelper(LBSTraceService lbsTraceService){
        this.lbsTraceService = lbsTraceService;
        if(locationListener == null){
            locationListener = new LocationListener(lbsTraceService.getContext());
            locationListener.startLocationRequest();
        }
    }

    public  static LocationListener getRealtimeLoc(Context context){
        if(locationListener == null){
            locationListener = new LocationListener(context);
            locationListener.startLocationRequest();
        }
        return locationListener;
    }

    public void startLocation(Context context) {
        TraceJniInterface.clearGPSData(context);
        if(locationListener == null){
            return;
        }
        LocationTrace locationTrace = null;
        locationTrace = locationListener.getLocationData();
        if(locationTrace != null && locationTrace.enableUser){
//            JsonUtil.toJson(locationTrace);
            String json = JsonTool.toJson(locationTrace);
            TraceJniInterface.setGPSTrackData(json,context);
        }
        //自定义数据
        if(locationTrace != null && locationTrace.enableUser){
            setCustom(context);
        }

        byte[] localData  = TraceJniInterface.buildLocData(context);
        if(localData == null){
            Log.e("TrackSDK","localData is null");
            return;
        }
        Log.e("localData is",new String(localData));
        if(gatherInterval < 5000 || locationTrace == null){

        }else{
            //将数据加入
            DataQueueManager.getInstance().addRealTimeDataQueue(localData);
        }
    }

    public   void removeUpdates() {
        if(locationListener != null) {
            locationListener.removeUpdates();
        }
    }

    public void setCustom(Context context){
        ArrayList arrayList = getTrackCustom();
        if(arrayList != null && arrayList.size() > 0) {
            for(int i = 0; i < arrayList.size(); ++i) {
                TrackCustom trackCustom = (TrackCustom)arrayList.get(i);
                TraceJniInterface.addCustomData(trackCustom.getKey(), trackCustom.getValue(),context);
            }
        }
    }

    /**
     * 自定义的回传数据
     * @return
     */
    private static ArrayList getTrackCustom() {
        ArrayList arrayList = new ArrayList();
        Map map = null;
        try {
            if(LBSTraceClient.onTrackListener != null && null != (map = LBSTraceClient.onTrackListener.onTrackAttrCallback())) {
                Iterator iterator = map.entrySet().iterator();
                while(iterator.hasNext()) {
                    Map.Entry var2 = (Map.Entry)iterator.next();
                    TrackCustom trackCustom = new TrackCustom();
                    trackCustom.setKey((String)var2.getKey());
                    trackCustom.setValue((String)var2.getValue());
                    arrayList.add(trackCustom);
                }
            }
        } catch (Exception var4) {
            arrayList.clear();
            arrayList = null;
        }
        return arrayList;
    }
}
